package com.huizhonglm.diap.widget.pickerView;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.ui.views.PopupMaskView;


/**
 * 滚轮控件
 *
 * @author LiXiaolong
 * @version 1.0
 * @since 2013-3-15 上午10:01:48
 */
public class PickerView extends PopupWindow {

    private View parentView = null;
    private Activity mContext = null;
    private WheelView leftWheelView = null;
    private WheelView rightWheelView = null;
    private String[] leftArray = null;
    private String[] rightArray = null;
    private OnConfirmedListener mOnConfirmedListener = null;
    private OnLeftScrollListener mOnLeftScrollListener = null;

    private PopupMaskView mask;

    public PickerView(Activity context, View parent) {
        super(context);
        parentView = parent;
        mContext = context;
        mask = new PopupMaskView(context, this);
    }

    /**
     * 弹出拨轮
     */
    public void show() {
        mask.showAtLocation(parentView);
        setDefaultSelected(null, null);
        if (!this.isShowing()) {
            this.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 弹出单排拨轮（有默认项）
     *
     * @param def 默认项
     */
    public void show(String def) {
        mask.showAtLocation(parentView);
        setDefaultSelected(def, null);
        if (!this.isShowing()) {
            this.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 弹出双排拨轮（有默认项）
     *
     * @param def_left  左边默认项
     * @param def_right 右边默认项
     */
    public void show(String def_left, String def_right) {
        mask.showAtLocation(parentView);
        setDefaultSelected(def_left, def_right);
        if (!this.isShowing()) {
            this.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 设置确定按钮点击事件监听
     *
     * @param listener 点击事件监听
     */
    public void setOnConfirmedListener(OnConfirmedListener listener) {
        this.mOnConfirmedListener = listener;
    }

    /**
     * 设置左边拨轮滑动事件监听
     *
     * @param listener 滑动事件监听
     */
    public void setOnLeftScrollListener(OnLeftScrollListener listener) {
        this.mOnLeftScrollListener = listener;
    }

    public void setWheelData(String[] singleData) {
        initArrayData(singleData, null);
        initPickerView(false);
    }

    public void setWheelData(String[] leftData, String[] rightData) {
        initArrayData(leftData, rightData);
        initPickerView(true);
    }

    /**
     * 更新右边拨轮数据
     *
     * @param rightData 右边拨轮数据
     */
    public void changeRightData(String[] rightData) {
        if (rightArray != null) {
            rightArray = null;
        }
        rightArray = rightData;
        setWheelColumnNum(2);
        rightWheelView.setVisibility(View.VISIBLE);
        if (rightArray != null && rightArray.length > 0) {
            rightWheelView.setAdapter(new ArrayWheelAdapter<>(rightArray));
            rightWheelView.setCurrentItem(0);
        } else {
            rightWheelView.setAdapter(null);
        }
    }

    /**
     * 初始化滚轮数据
     */
    private void initArrayData(String[] leftData, String[] rightData) {
        leftArray = leftData;
        rightArray = rightData;
    }

    /**
     * 初始化pickerView
     */
    private void initPickerView(boolean isDouble) {
        View contentView = View.inflate(mContext, R.layout.widget_picker_view, null);
        leftWheelView = (WheelView) contentView.findViewById(R.id.left_whellView);
        rightWheelView = (WheelView) contentView.findViewById(R.id.right_whellView);
        Button confirmBtn = (Button) contentView.findViewById(R.id.common_btn);
        Button cancelBtn = (Button) contentView.findViewById(R.id.cancel_btn);
        contentView.setFocusableInTouchMode(true);
        contentView.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && (isShowing())) {
                    dismiss();
                    return true;
                }
                return false;
            }
        });
        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                int leftID = leftWheelView.getCurrentItem();
                int rightID;
                String left;
                String right = "";
                if (rightArray != null && rightArray.length > 0) {
                    rightID = rightWheelView.getCurrentItem();
                    right = rightArray[rightID];
                }
                left = leftArray[leftID];
                if (mOnConfirmedListener != null) {
                    mOnConfirmedListener.onConfirmed(left, right);
                }
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        leftWheelView.setAdapter(new ArrayWheelAdapter<>(leftArray));
        leftWheelView.addScrollingListener(getScrollListener(rightWheelView));
        if (isDouble) {
            changeRightData(rightArray);
        } else {
            setWheelColumnNum(1);
            rightWheelView.setVisibility(View.GONE);
        }
        setFocusable(true);
        setContentView(contentView);
        setWidth(LayoutParams.FILL_PARENT);
        setHeight(LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new BitmapDrawable());
        setAnimationStyle(R.style.bottom_view_anim_style);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                mask.dismiss();
            }
        });
        update();
    }

    private OnWheelScrollListener getScrollListener(final WheelView anOther_wheel) {
        return new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                anOther_wheel.setCurrentItem(0);
                if (mOnLeftScrollListener != null) {
                    mOnLeftScrollListener.onScrollingFinished(wheel.getCurrentItem());
                }
            }
        };
    }

    private void setWheelColumnNum(int columnNum) {
        if (leftWheelView != null && rightWheelView != null) {
            leftWheelView.setColumnNum(columnNum);
            rightWheelView.setColumnNum(columnNum);
        }
    }

    private void setDefaultSelected(String def_left, String def_right) {
        int index_l = 0;
        int index_r = 0;
        if (def_left != null && def_right != null) {
            int i = 0;
            for (String left : leftArray) {
                if (def_left.equals(left)) {
                    index_l = i;
                    break;
                }
                i++;
            }
            int j = 0;
            for (String right : rightArray) {
                if (def_right.equals(right)) {
                    index_r = j;
                    break;
                }
                j++;
            }
        } else if (def_left != null && def_right == null) {
            int i = 0;
            for (String left : leftArray) {
                if (def_left.equals(left)) {
                    index_l = i;
                    break;
                }
                i++;
            }
        }
        leftWheelView.setCurrentItem(index_l);
        rightWheelView.setCurrentItem(index_r);
    }

    /**
     * 确认所选择的数据
     *
     * @author LiXiaoLong
     * @version 1.0
     * @since 2013年7月30日 下午1:13:38 (1.0)
     */
    public interface OnConfirmedListener {
        /**
         * 确认已选择数据
         *
         * @param leftSelected  左边滚轮数据
         * @param rightSelected 右边滚轮数据
         */
        public void onConfirmed(String leftSelected, String rightSelected);
    }

    /**
     * 左边滑动监听器
     *
     * @author LiXiaoLong
     * @version 1.0
     * @since 2013年7月30日 下午1:40:36 (1.0)
     */
    public interface OnLeftScrollListener {
        /**
         * 停止滑动
         *
         * @param leftID 左边选中项ID
         */
        public void onScrollingFinished(int leftID);
    }

}
